import Vue from 'vue'
import Router from 'vue-router'
import LazyLoad from './lazyLoad'
import Layout from '@/layout'
import error404 from '@/errorPage/404'

const modulesFiles = require.context('./modules', true, /\.js$/)
const modules = modulesFiles.keys().map(modulePath => modulesFiles(modulePath).default)
Vue.use(Router)

export const constantRoutes = [
  {
    path: '/404',
    component: error404
  },
  {
    path: '',
    component: Layout
  },
  {
    path: '/login',
    component: LazyLoad('Login')
  }
]

export const asyncRoutes = [
  ...modules,
  { path: `*`, redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  base: '/',
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
